Employee Updater: Building a Client-Server Application with Macromedia Flash Data Connection Wizard and ColdFusion
Table of Contents
Processing Updates
The Flash side of the application is complete, but what about the server side? Right now the application dumps changes to an XML file, but you need ColdFusion code to process the uploaded delta packets and update the databases.
The complete Update method, the one that actually updates
the tables, is too lengthy to include here. You can download it from
the sample files at the beginning
of this tutorial, but I explain the key parts here.
The method received the delta packet from Flash as an argument:
<CFARGUMENT NAME="deltaData" TYPE="string" REQUIRED="true">
The code then parses the delta packet, and uses an XPath search to obtain an array of nodes. Each insert, delete, and update will be in its own node:
<!--- Parse received XML packet ---> <CFSET deltaXML=XMLParse(deltaData)> <!--- Extract all nodes (each operation is a node) ---> <CFSET deltaNodes=XmlSearch(deltaXML, "update_packet/*")> <!--- How many operations? ---> <CFSET nodeCount=ArrayLen(deltaNodes)>
The code then loops through from one to nodeCount to process each action. The following code is the first line within the loop, determining the action by looking at the node name:
<!--- Get action (INSERT UPDATE or DELETE) ---> <CFSET action=deltaNodes[i].XmlName>
If the action is an UPDATE or a DELETE then the logic requires the
primary key. It uses another XPath search to find all fields with key
set to true (this based on the field specified in the Resolver previously).
Once processed, the variable pkid will contain the primary
key value so that you can use it in constructed SQL statements:
<!--- Get primary key if update or delete ---> <CFIF (action IS "update") OR (action IS "delete")> <!--- Get node with key field ---> <CFSET keyFldNodes=XmlSearch(deltaXML, "update_packet/*[#i#]/field[@key='true']")> <!--- Get primary key (in "OldValue") ---> <CFSET pkid=keyFldNodes[1].XmlAttributes.oldValue> </CFIF>
Then the application performs one XPath search per loop to obtain all the non-key fields, where key is false:
<!--- Now get all non-key fields ---> <CFSET modFldNodes=XmlSearch(deltaXML, "update_packet/*[#i#]/field[@key='false']")>
Armed with all this information, the code can construct a SQL statement:
- For
UPDATEstatements, the logic creates the opening SQL, appends eachname=valuepair, and finally adds theWHEREclause. - For
DELETEstatements, the logic generates a simpleDELETEusing the retrieved primary key. - For
INSERTstatements, the logic populates two lists that contain field names and values, generates a new primary key (a UUID), and generates the complete SQLINSERTstatement.
At the bottom of the loop, the logic appends the new SQL statement to an array of SQL statements:
<!--- Add this statement to the array of SQL statements ---> <CFSET ArrayAppend(sqlStatements, sql)>
Then it creates a CFTRANSACTION tag block that contains
CFLOOP tag which loops over the array and submits a SQL
query for each entry, as follows:
<!--- And run the query ---> <CFQUERY DATASOURCE="exampleapps"> #PreserveSingleQuotes(sql)# </CFQUERY>
The method invoked by the Resolver expects a result packet. (This could contain error messages if specific SQL statements failed, or other information that could be used by Flash on the client.) For simplicity's sake, the CFC method in this example returns an empty packet:
<!--- Return result packet (this one is empty) ---> <CFRETURN "<result_packet />">
Now ColdFusion can process the delta packet and update the database in a single transaction.
Summary
And there you have it: a complete client-server application—the client in Flash and the server in ColdFusion—oh, and the entire application is under 150K at runtime! While I covered a lot of concepts and ideas in this article, the truth is that is does not get much more complex than this. Most of what you'll need to do when building your own ColdFusion/Flash applications will be similar to what you did here today.
There is definitely a learning curve here, especially for those of us who have feared Flash thus far. But as you can see by the finished product, the investment is well worth it.
Where To Go From Here
You now have a complete working application, an impressive and useful example of ColdFusion and Flash working together to create a better user experience. If you'd like to experiment further, here are some suggested application enhancements:
- As already noted above, the employee data grid should not really
display the
EMPLOYEEIDandDEPTIDFKcolumns. You can run the DataGrid Column Editor wizard to correct this. - The tables contain additional employee information that does not display in the grid (including a notes field). You may want to change the screen layout so that the department grid is on the left (make it narrower than it is now), the employees grid on the top right, and a selected employee's details on the bottom right. To display the additional details you'd need to add controls to display employee details, and then bind each control to the employee data set. You'd also want to list the extra columns in the Data Connection Wizard (and then use the DataGrid Column Editor to prevent them from displaying).
- Right now, a user edits within the employee data grid, but what if the user wanted to edit other columns not displayed in the grid? You may want to add an Edit button to the navLogout page, and have it pop-up an edit form (which a user could use in addition to quick inline editing). You'd need to create a new form, populate it with controls, and bind them to a new CFC method to update the employee. Or you could combine this idea with the previous bullet for employee detail controls, and allow users to edit those if they're logged in.
- The
Update()method may succeed or fail, but ColdFusion doesn't return success or failure notifications. You may want changeUpdate()method so that it builds an XML result packet which it sends to Flash. You could then trap the web wervices on (result) event and inspect the returned packet, displaying a notification window if for some reason theUpdate()method fails. - While this app works, there is an inherent security hole; you
should not deploy this application as it is on a public facing
site. The CFC
Update()method accepts a deltaPacket with updates. In our example that packet comes from client-side Flash, but if hackers were aware of the CFC and the format of the deltaPacket, they could submit packets of their own. The solution to this is to use server-side session-state management so that the log-on is not just an on/off state which swaps forms and makes grids editable, but rather, the CFC remembers logged in users and only acceptsUpdate()requests from them.
As you can see, there are lots of possibilities and opportunities. To learn more you may want to take a look at these resources:
- Flash Data Integration Resources
- Flash Component Resources
- Bike Trips Sample Application: Using the XMLConnector Component in Flash MX Professional 2004 by Leo Schumann, J.D.
- Introduction to CFCs by Ben Forta
- ColdFusion Components page in the ColdFusion Developer Center
- Choosing Between XML, Web Services, and Remoting for Rich Internet Applications by Steven Webster
- Getting a Handle on Web Services in Macromedia Flash MX Professional 2004 by Vera Fleischer